package com.all.common.services;

import com.all.common.gps.GpsLocation;
import com.all.utils.ReflectionUtils;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class GpsDataService extends Service {

	protected final String TAG = this.getClass().getName();
	protected Context mContext;
	// gps定位类
	GpsLocation mGpsLocation;

	Handler mHandler;

	String mClassName;
	// 标识位 0:通过handler 1:通过broadcast
	private static int flag = 0;

	 private IBinder binder=new GpsDataService.LocalBinder();
	public static int getFlag() {
		return flag;
	}

	public static void setFlag(int flag) {
		GpsDataService.flag = flag;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mContext = this;
		mGpsLocation = new GpsLocation(mContext);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "service on binded.");
		mClassName = intent.getStringExtra("className");
		try {
			mHandler = (Handler) ReflectionUtils.invokeStaticMethod(mClassName,
					"getHandler", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		startWork();
		return binder;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onStartCommand.");
		mClassName = intent.getStringExtra("className");
		try {
			mHandler = (Handler) ReflectionUtils.invokeStaticMethod(mClassName,
					"getHandler", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		startWork();
		return super.onStartCommand(intent, flags, startId);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mThread.stop();
		if(mGpsLocation!=null)
		{
			mGpsLocation.removeListener();
			mGpsLocation=null;
		}
		
	}

	private void startWork() {
		// TODO Auto-generated method stub
		/*
		 * 定位工作
		 */
		mThread.start();
	}

	Thread mThread = new Thread(new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			/*
			 * 与activity进行通信
			 */
			Message msg;
			Location location = mGpsLocation.getLocation();
			if (flag == 0) {
				if (mHandler != null) {
					if (location != null)
						msg = Message.obtain(mHandler, 1, location);
					else
						msg = Message.obtain(mHandler, 0);
					msg.sendToTarget();
				} else
					Log.i(TAG, "the activity handler is empty.");
			} else {
				Intent intent = new Intent("com.gps.location");
				sendBroadcast(intent);
			}

			/*
			 * 当定位返回为空时，重新进行定位注册
			 */
			while (location == null) {
				mGpsLocation.requertLoc();
			}

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	});
	
	//定义内容类继承Binder
    public class LocalBinder extends Binder{
        //返回本地服务
    	GpsDataService getService(){
            return GpsDataService.this;
        }
    }
}
